/*
    用来发送数据到微信接口
*/
public class CaseWeixinHandler implements Triggers.Handler {
    public void handle() {
        //在新建个案后和更新后执行
        if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
            //需要发送数据到微信的个案列表
            List<Case> postList = new List<Case>();
            //工程师出发
            Set<ID> postSet = new Set<ID>();
            //工单完成
            Set<ID> postfishSet = new Set<ID>();
            //个案的工程师Id列表
            Map<Id,String> engineerIds = new Map<Id,String>();
            //保存个案指定的工程师
            Map<Id,WorkOrder__c> oldMap = (Map<Id,WorkOrder__c>)Trigger.oldMap;
            for(WorkOrder__c wo :(List<WorkOrder__c>)Trigger.new){
                if (wo.Status__c != oldMap.get(wo.Id).Status__c ) {
                   // engineerIds.put(wo.Id,wo.Technician__c);
                  //  
                    if(wo.Status__c == '接单')postSet.add(wo.Case__c);
                    if(wo.Status__c == '完成')postfishSet.add(wo.Case__c);
                }

            }
            if(postSet.size() >0){
                for (Case c:[select Id ,WeChatRepairName__c,CreatedBy.Name from Case where Id in: postSet and Origin = '扫码报修' ] ) {
                    String msg = c.WeChatRepairName__c +'您好! 工程师已于'+Utils.date2string(System.now())+'出发!';
                    Utils.postCaseStatusToWeiXin(c.Id,msg,' ');
                }
            }
            
            if(postfishSet.size() > 0){
                for (Case c:[select Id ,Account.Name,Subject from Case where Id in: postfishSet and Origin = '扫码报修' ] ) {
                    String msg = '对' +c.Account.Name +'的仪器的'+c.Subject+'已处理完成!';
                    Utils.postCaseStatusToWeiXin(c.Id,msg,' ');
                }

            }
             

           /* Map<Id,User> mapIds = [select Id, Name from User where Id IN:engineerIds.values()];
            for (Id wo  : engineerIds.keySet()) {
                mapIds.get(engineerIds.get(wo));
                String e = 
            }*/
            /*
            for (Case w : (List<Case>)Trigger.new){
                //如果个案来源为【微信】则要POST数据到微信接口
                if(w.Origin == '扫码报修'){
                    postList.add(w);
                    if(w.Engineer__c != null){
                        engineerIds.add(w.Engineer__c);
                    }
                }
            }
            if(engineerIds.size() > 0){
                mapIds = new Map<String,User>([SELECT Id,Name FROM User WHERE Id IN : engineerIds]);
            }
            //循环发送数据到微信接口
            for(Case w : postList){
                String eid = '';
                if(w.Engineer__c != null && mapIds.containsKey(w.Engineer__c)){
                    eid = mapIds.get(w.Engineer__c).Name;
                }
            }*/
        }
    }
}